package com.components.media
{
	/**
	 *  @auth Chunning
	 *  @purpose view of MediaPlayer...
	 *  @date 2009-3-19
	 * 
	 * */	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.EventDispatcher;
	import flash.media.Video;
	import flash.net.NetStream;
	
	import utils.display.MCUtils;

	public class MediaPlayerView extends EventDispatcher
	{
		//START
		
		//PROPERTY
		private var target:Sprite;
		
		private var background:Sprite;
		
		private var buffering:DisplayObject;
		
		private var videoMask:Shape;
		
		private var videoContainer:Sprite;
		
		private var border:DisplayObject;
		
		internal var video:Video;		
		
		//CONSTRUCTOR
		public function MediaPlayerView(target:Sprite)
		{
			
			this.target = target;
			
			viewInit();
						
		}
		
		//METHODS...
			
		//view init...
		private function viewInit():void
		{
			
			this.background = MCUtils.getInstance(this.target,"background") as Sprite;
			
			this.buffering = MCUtils.getInstance(this.target,"buffering");
			
			border = MCUtils.getInstance(this.target,"border");
			
			videoContainer = MCUtils.getInstance(this.target,"container") as Sprite;
			
			this.background.mouseEnabled = false;
			
						
		}
		
		//init player view...
		public function initPlayerView():void
		{
			
			if(this.buffering is DisplayObject)
			{
				
				buffering.visible = false;
				
			}
			
			this.video = new Video;
			
			videoContainer.addChild(this.video);
			
			var panel:I_PlayerCtrlPanel = MCUtils.getInstance(this.target,"ctrlPanel") as I_PlayerCtrlPanel;
									
			createMask();
			
		}
		
		//create mask...
		private function createMask():void
		{
			
			this.videoMask = new Shape;
			
			this.videoMask.graphics.beginFill(0xFF9900,0.5);
			
			if(this.background is Sprite)
			{
				
				this.videoMask.graphics.drawRect(this.background.x,this.background.y,this.background.width,this.background.height);
				
			}else{
				
				this.videoMask.graphics.drawRect(0,0,10,10);
				
			}
			
			this.videoMask.graphics.endFill();
			
			videoContainer.addChild(this.videoMask);
			
			this.video.mask = this.videoMask;
			
		}
		
		//set bg height...
		public function set bgHeight(val:Number):void
		{
			
			if(this.background is Sprite)
			{
				
				this.background.height = val;
				
				this.videoMask.height = val;
				
				if(border) border.height = val;
				
				if(buffering)buffering.y = val / 2;
				
			}			
			
		}
		
		//get bg height...
		public function get bgHeight():Number
		{
			
			if(!(this.background is Sprite))
			{
				
				return 0;
				
			}
			
			return this.background.height;
			
		}
		
		//set bg width...
		public function set bgWidth(val:Number):void
		{
			
			if(this.background is Sprite)
			{
				
				this.background.width = val;
				
				this.videoMask.width = val;
			
				if(border) border.width = val;
				
				if(buffering)buffering.x = val / 2;
				
			}
									
		}
		
		//get bg width...
		public function get bgWidth():Number
		{
			
			if(!(this.background is Sprite))
			{
				
				return 0;
				
			}
			
			return this.background.width;
			
		}
		
		
				
		//attach ns  to a Video Object...
		public function attachNS(ns:NetStream):void
		{
															
			this.video.attachNetStream(ns);
						
		}
				
		
		//set video size...
		public function setVideoSize(width:Number,height:Number):void
		{
							
			this.video.width = width;
			
			this.video.height = height;
			
			if(this.bgWidth > 0 && this.bgHeight >0)
			{
								
				this.video.x = this.background.x + (this.bgWidth - width) / 2;
			
				this.video.y = this.background.y + (this.bgHeight - height) / 2;
				
			}else{
				
				
				this.videoMask.width = width;
				
				this.videoMask.height = height;
				
				this.video.x = 0;
			
				this.video.y = 0;
				
			}
			
		}
		
		//show buffering...
		public function showBuffer():void
		{
			
			buffering.visible = true;
			
		}
		
		
		//hide buffering.
		public function hideBuffer():void
		{
			
			buffering.visible = false;
			
		}
				
		
		//END
	}
}